package com.demo.dao.mapper;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.demo.model.Teacher;

public class TeacherMapper {

	public String insert(Teacher teacher) {
		return "insert into teacher (name, age) values (#{name},#{age})";
	}

	public String delete(long id) {
		BEGIN();
		DELETE_FROM("teacher");
		WHERE("id=#{id}");
		return SQL();
	}

	public String update(Teacher teacher) {
		BEGIN();
		UPDATE("teacher");
		if (teacher.getName() != null && teacher.getName().length() > 0) {
			SET("name=#{name}");
		}
		if (teacher.getAge() != null) {
			SET("age=#{age}");
		}
		WHERE("id=#{id}");
		String sql = SQL();
		System.out.println(sql);
		return sql;
	}

	private void where(String name) {
		if (name != null && name.length() > 0) {
			WHERE("name=#{name}");
		}
	}
	
	public String select(Map<String, Object> parameter) {
		String name = (String) parameter.get("name");
		int pageNo = (int) parameter.get("pageNo");
		int pageSize = (int) parameter.get("pageSize");
		
		BEGIN();
		SELECT("*");
		FROM("teacher");
		where(name);
		ORDER_BY("id desc");
		return SQL() + " limit " + pageNo * pageSize + ", " + pageSize;
		
	}
//		if (name == null || name.length() == 0) {
//			return "select * from teacher order by id desc";
//		} else {
//			return "select * from teacher where name=#{name} order by id desc";
//		}
//	}
	
	public String count(String name) {
		BEGIN();
		SELECT("count(*)");
		FROM("teacher");
		where(name);
		return SQL();
	}

	public String selectOne(Integer id) {
		return "select * from teacher where id=#{id}";
	}

}
